package cn.lin.controller;

import cn.lin.domain.Catalog;
import cn.lin.domain.Module;
import cn.lin.service.CatalogService;
import cn.lin.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author cn.Lin
 * @create 2024-12-03-11:19
 */
@RestController
@RequestMapping("/catalog")
public class CatalogController {

    @Autowired
    private CatalogService catalogService;

    /**
     * 返回对应班级课程目录
     * @param classId 班级id
     */
    @GetMapping("/showClassCatalog")
    public Object showClassCatalog(@RequestParam("id") Integer classId){
        List<Module> catalogList = catalogService.findCatalogByClassId(classId);
        if (catalogList == null){
            return Result.error("查询失败");
        }
        return Result.ok("查询成功",catalogList);
    }

    /**
     * 通过剪辑具体的视频。获取id，拿到对应的url（进目录表）
     * @param id
     * @return
     */
    @RequestMapping("/showVideo")
    public Object showVideo(@RequestParam("id") Integer id){
        Catalog catalog = catalogService.showVideo(id);
        if (catalog == null){
            return Result.error("查询失败") ;
        }
        String url = catalog.getFileName();
        return Result.ok("查询成功",url) ;
    }

    @RequestMapping("/addModule")
    public Object addModule(@RequestBody Catalog catalog){
        boolean flag = catalogService.addModule(catalog);
        if (flag){
            return Result.ok("添加成功");
        }
        return Result.error("添加失败");
    }

    @RequestMapping("/addClassHours")
    public Object addClassHours(@RequestBody Catalog catalog){
        boolean flag = catalogService.addClassHours(catalog);
        if (flag){
            return Result.ok("添加成功");
        }
        return Result.error("添加失败");
    }

    @RequestMapping("/deleteModule")
    public Object deleteModule(@RequestParam("id") Integer id){
        boolean flag = catalogService.deleteModule(id);
        if (flag){
            return Result.ok("删除成功");
        }
        return Result.error("删除失败");
    }

}
